package com.dreamtour.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dreamtour.DTO.DailyTransactionDTO;
import com.dreamtour.entity.Transactions;
import com.dreamtour.mapper.TransactionsMapper;
import com.dreamtour.service.ITransactionsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xls
 * @since 2024-12-24
 */
@Service
public class TransactionsServiceImpl extends ServiceImpl<TransactionsMapper, Transactions> implements ITransactionsService {
    @Autowired
    TransactionsMapper transactionsMapper;
    public List<DailyTransactionDTO> getDailyTransactionStats() {
        return transactionsMapper.getDailyTransactionStats();
    }

    @Override
    public Integer getTransactionCountByRange(int min, int max) {
        return Math.toIntExact(transactionsMapper.selectCount(
                new QueryWrapper<Transactions>()
                        .ge("transactionAmount", min)
                        .lt("transactionAmount", max)
        ));
    }
    public Integer getTransactionCountAbove(int min) {
        return Math.toIntExact(transactionsMapper.selectCount(
                new QueryWrapper<Transactions>()
                        .ge("transactionAmount", min)
        ));
    }

    public Map<Integer, Integer> getUserRatingStats(){
        Map<Integer, Integer> ratingStats = new HashMap<>();
        for (int rating = 1; rating <= 5; rating++) {
            // 按评分统计每个评分的交易次数
            int count = Math.toIntExact(transactionsMapper.selectCount(
                    new QueryWrapper<Transactions>()
                            .eq("userRating", rating)
            ));
            ratingStats.put(rating, count);
        }
        return ratingStats;
    }
}
